document.addEventListener('DOMContentLoaded', function() {
    // 文本分析表单处理
    const analyzeForm = document.getElementById('analyzeForm');
    const analysisResult = document.getElementById('analysisResult');
    
    analyzeForm.addEventListener('submit', async function(e) {
        e.preventDefault();
        const text = document.getElementById('text').value;
        
        try {
            const response = await fetch('/api/analyze', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ text })
            });
            
            const data = await response.json();
            
            if (response.ok) {
                analysisResult.innerHTML = `
                    <div class="alert alert-success">
                        <h6>分析结果：</h6>
                        <p>情感倾向：${data.sentiment}</p>
                        <p>关键词：${data.keywords.join(', ')}</p>
                    </div>
                `;
            } else {
                analysisResult.innerHTML = `
                    <div class="alert alert-danger">
                        ${data.error}
                    </div>
                `;
            }
        } catch (error) {
            analysisResult.innerHTML = `
                <div class="alert alert-danger">
                    分析过程中发生错误
                </div>
            `;
        }
    });
    
    // 数据采集表单处理
    const crawlForm = document.getElementById('crawlForm');
    const crawlResult = document.getElementById('crawlResult');
    
    crawlForm.addEventListener('submit', async function(e) {
        e.preventDefault();
        const url = document.getElementById('url').value;
        
        try {
            const response = await fetch('/api/crawl', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ url })
            });
            
            const data = await response.json();
            
            if (response.ok) {
                let newsHtml = '<div class="list-group">';
                data.news.forEach(item => {
                    newsHtml += `
                        <div class="list-group-item">
                            <h6>${item.title}</h6>
                            <p class="mb-1">${item.content.substring(0, 100)}...</p>
                            <small>来源：${item.source}</small>
                        </div>
                    `;
                });
                newsHtml += '</div>';
                
                crawlResult.innerHTML = newsHtml;
            } else {
                crawlResult.innerHTML = `
                    <div class="alert alert-danger">
                        ${data.error}
                    </div>
                `;
            }
        } catch (error) {
            crawlResult.innerHTML = `
                <div class="alert alert-danger">
                    采集过程中发生错误
                </div>
            `;
        }
    });
}); 